/*****************************************************
 * 工作流程管理，主要管理流程的发起，审批，退回，等流程操作
 * 
 * 
 */
var WORKFLOW_MANAGER = {
		/**
		 * 流程配置信息
		 */
		workFlowConfig: null,
		/**
		 * 启动工作流程
		 */
		startWorkFlowProcess: function(config){
			var opts = {
					//流程编码
					workflow_code: "",
					//流程名称
					workflow_name: "",
					//部署流程编码
					workflow_deployed_id: "",
					//部署流程名称
					workflow_deployed_name: "",
					//请求流程id
					request_id: "",
					//请求流程名称
					request_name: "",
					//流程启动成功后执行事件
					call: Ext.emptyFn
			};
			
			Ext.apply(opts,config);
			this.workFlowConfig = opts;
			AjaxUtils.CallService({
				url: CONTEXT.contextPath+"/wf/v1/startworkflow",
				params: {
					workflow_code: WORKFLOW_MANAGER.workFlowConfig.workflow_code,
					workflow_deployed_id: WORKFLOW_MANAGER.workFlowConfig.workflow_code
				},
				call: function(data){
					if(data.status == "OK"){
						WORKFLOW_MANAGER.workFlowConfig.request_id = data.entity.request_id;
						WORKFLOW_MANAGER.workFlowConfig.request_name = data.entity.request_name;
						WORKFLOW_MANAGER.workFlowConfig.workflow_name = data.entity.workflow_name;
						WORKFLOW_MANAGER.workFlowConfig.workflow_deployed_name = data.entity.workflow_deployed_name;
						//打开流程界面
						WORKFLOW_MANAGER.openWorkFlowProcessWindow();
					}else{
						AjaxUtils.showError('流程启动失败:'+data.message);
					}
				}
			});
		},
		/**
		 * 打开工作流窗体
		 */
		openWorkFlowProcessWindow: function(){
			var workFlowWin = Ext.create('Ext.com.bkfm.window', {
				title: "",
				autoScroll : true,
				modal : true,
				width: Utils.getWidth()*0.98,
				height: Utils.getHeight()*0.98,
				layout: {
	                type: 'border'
	            },
	            items:[{
	            	id: "workflow_main_panel",
	            	region: 'west',
	            	width: (Utils.getWidth()*0.98)*0.8,
	                border: false,
	                layout: {
						type: "table",
						columns: 1,
						forceFit : true
					},
					items:[]
	            },{
	            	region: 'center',
	                border: false,
	                layout: {
		                type: 'border'
		            },
		            items:[{
		            	region: 'north',
		            	layout: 'fit',
		            	title: "附件列表",
		            	height: (Utils.getHeight()*0.98)*0.4,
		            	items:[WORKFLOW_MANAGER.createWorkFlowFilesGrid()]
		            },{
		            	region: 'center',
		            	layout: 'fit',
		            	title: "审批历史",
		            	items:[]
		            }]
	            }]
			});
			workFlowWin.show(this,function(){
				//alert(0);
				WORKFLOW_MANAGER.showWorkFlowForms();
			});
		},
		/**
		 * 显示业务流程表单页面
		 */
		showWorkFlowForms: function(){
			
		},
		/**
		 * 创建流程附件
		 */
		createWorkFlowFilesGrid: function(){
			return Ext.create('Ext.com.bkfm.datagrid', {
				id: "workflow_files_grid",
				objPath : "ROOT/WORKFLOW/YXLCGL/BLK_WORKFLOW_FILES",
				border: 0,
				frame: false,
				height: (Utils.getHeight()*0.98)*0.3,
				showstandardBtn: false,
				hiddenField:["WORKFLOW_DEPLOYED_ID","FILE_ID","FILE_TYPE","FILE_SIZE"],
				tbar:[{
					text: "上传",
					iconCls: 'attachment_upload',
					handler: function(){
						AjaxUtils.CallFileUploadService(function(result,uploadWin){
			    			var param = new Array();
			    			for(i in result.list){
			    				var file = result.list[i];
								param.push({
									"obj_path": "ROOT/WORKFLOW/YXLCGL/BLK_WORKFLOW_FILES",
									"record_state": Constants.RECORD_NEW,
									"rwid": Utils.getRwid(),
									"record_data":Ext.JSON.encode({
										"FILE_NAME": file.file_name,
										"FILE_TYPE": file.file_ext,
										"FILE_SIZE": file.file_size,
										"FILE_ID": file.file_id
									})
								});
			    			}
			    			AjaxUtils.CallService({
								url: CONTEXT.contextPath+"/bkfm/metadata/v1/savemodelmetadata",
								dataType: "json",
								params: param,
								call: function(data){
									if(data.status == "OK"){
										AjaxUtils.showInfo('操作成功',function(){
											Ext.getCmp("workflow_files_grid").reload();
											uploadWin.close();
										});
									}else{
										AjaxUtils.showError('附件上传失败');
									}
								}
							});
			    			
			    		});
					}
				},{
					text: "下载",
					iconCls: "attachment_download",
					handler: function(){
						var records = Ext.getCmp("workflow_files_grid").getSelectedRecords();
						if(records.length ==1){
							debugger;
							AjaxUtils.CallService({
								url: CONTEXT.contextPath+"/bkfm/common/v1/downloadfile",
								params: {
									file_id: records[0].get("FILE_ID")
								},
								call: function(data){
									if(data.status == "OK"){
										var path = data.entity;
										Ext.create('Ext.com.bkfm.window', {
											title: "附件下载",
											autoScroll : true,
											modal : true,
											width: Utils.getWidth()*0.5,
											height: Utils.getHeight()*0.4,
											bodyStyle:'padding:10px 10px 10px 10px',
											html: '<a href="'+CONTEXT.contextPath+'/bkfm/common/download?downloadInfo='+encodeURIComponent(path)+'">附件生成成功，点击下载</a>',
										}).show();
									}else{
										AjaxUtils.showError('附件下载失败');
									}
								}
							});
						}else if(records.length >1){
							var fileid_list = [];
							for(i in records){
								var record = records[i];
								fileid_list.push(record.get("FILE_ID"));
							}
							AjaxUtils.CallService({
								url: CONTEXT.contextPath+"/bkfm/common/v1/downloadfilezip",
								dataType: "json",
								params: fileid_list,
								call: function(data){
									if(data.status == "OK"){
										var path = data.entity;
										Ext.create('Ext.com.bkfm.window', {
											title: "附件下载",
											autoScroll : true,
											modal : true,
											width: Utils.getWidth()*0.5,
											height: Utils.getHeight()*0.4,
											bodyStyle:'padding:10px 10px 10px 10px',
											html: '<a href="'+CONTEXT.contextPath+'/bkfm/common/download?downloadInfo='+encodeURIComponent(path)+'">附件生成成功，点击下载</a>',
										}).show();
									}else{
										AjaxUtils.showError('附件下载失败');
									}
								}
							});
						}else{
							AjaxUtils.showError('请选择需要下载的附件');
						}
					}
				},{
					text: "删除",
					iconCls: "delete",
					handler: function(){
						Ext.getCmp("workflow_files_grid").deleteRecords();
					}
				}]
			});
		}
}